#include "MailControl.h"

//convert from int to str
string int2str(int number)
{
   stringstream ss;
   ss << number;
   return ss.str();
}

string encrypt(string pw){
    char *ptr = &pw[0];
    
    while (*ptr != '\0')
    {
        *ptr += 3;
        ptr++;
    }
    
    return pw;
}

string decrypt(string pw){
    char *ptr = &pw[0];
    
    while (*ptr != '\0')
    {
        *ptr -= 3;
        ptr++;
    }
    
    return pw;
}

//creates a long time type to store secs elapsed since 1970
//time_t longtime;

//this sets the longtime var to currenttime
//time (&longtime);

void printd(long a){
    time_t time = a;

    //the tm struct is in secs, mins, hours, days, etc.. instead of
    //time_t which is only secs elapsed.
    struct tm * timeinfo;

    //the localtime() converts the long type to a tm struct
    timeinfo = localtime (&time);

    //this asctime() converts tm struct to string
    //the string prints the date and time with newline!!!
    //e.g. Thu Aug 23 14:55:02 2001
    cout << asctime(timeinfo);

    //this stopped working after switching to MinGW
    //char buffer[80];
    //strftime(buffer,80,"%a %b %d %T %G",timeinfo);
    //cout << buffer;
}

mailController::mailController() // constructor
{
    ifstream infile;
    string mids, msg, ckey, dates, rStatuss;
    bool rStatus;
    int mid;
    long date;

    infile.open("mailmsg.txt");
    while (infile >> mids){
        mid = atoi(decrypt(mids).c_str());
        getline(infile, msg);
        msg.erase(0,1);
        msg = decrypt(msg);
        mailMsg temp(mid, msg);
        vmm.push_back(temp);
    }
    infile.close();

    infile.open("mailbox.txt");
    deque<im> tdim;
    while (infile >> ckey){
        ckey = decrypt(ckey);
        tdim.clear();
        for (int i = 0; i<10; i++){
            infile >> dates;
            date = atol(decrypt(dates).c_str());
            infile >> rStatuss;
            rStatus = atoi(decrypt(rStatuss).c_str());
            infile >> mids;
            mid = atoi(decrypt(mids).c_str());
            im tim;

            tim.mdate = date;
            tim.rStatus = rStatus;
            tim.imid = mid;
            tdim.push_back(tim);
        }
        mailBox temp(ckey, tdim);
        vmb.push_back(temp);
    }
    infile.close();
}

mailController::~mailController()//used to write to file
{
    ofstream outfile;

    pmm = vmm.begin();
    outfile.open("mailmsg.txt");
    while (pmm !=vmm.end()){
        outfile << encrypt(int2str(pmm->mid))<<"\t";
        outfile << encrypt(pmm->msg) <<"\r\n";
        pmm++;
    }
    outfile.close();

    pmb = vmb.begin();
    outfile.open("mailbox.txt");

    while (pmb !=vmb.end()){
        outfile << encrypt(pmb->ckey) << "\r\n";
        deque<im>::iterator pim = pmb->dim.begin();
        while (pim !=pmb->dim.end()){
            outfile << encrypt(int2str(pim->mdate)) << "\r\n"
                    << encrypt(int2str(pim->rStatus)) << "\r\n"
                    << encrypt(int2str(pim->imid)) << "\r\n";
            pim++;
        }
        pmb++;
    }
    outfile.close();
}

//this is called once a new customer acc is created
void mailController::createMailbox (string a){
    deque<im> tdim;
    for (int i = 0; i<maxmail; i++){
        im tim;
        tim.mdate = 0;
        tim.rStatus = 0;
        tim.imid = 0;
        tdim.push_back(tim);
    }
    mailBox temp(a, tdim);
    vmb.push_back(temp);
}

void mailController::deleteMailbox (string a){
    pmb = vmb.begin();
    while (pmb !=vmb.end()){
        if (a == pmb->ckey){
            vmb.erase(pmb);
            break;
        }
        else
            pmb++;
    }
}

void mailController::sendBrochure (string a){
    int b;
    pmm = vmm.end()-1;
    b = (pmm->mid) + 1;
    mailMsg temp(b, a);
    vmm.push_back(temp);

    pmb = vmb.begin();
    time_t nowtime;
    time (&nowtime);

    im tim;
    tim.mdate = nowtime;
    tim.rStatus = 0;
    tim.imid = b;

    while (pmb != vmb.end()){
        pmb->dim.push_front(tim);
        pmb->dim.pop_back();
        pmb++;
    }
}

void mailController::sendMail(int a, string b){
//int a:
//    1 = send payment for checked bookings (Initiated from FO)
//    2 = send payment for canceled bookings (Initiated from C)
//string b will be the customer's ID
    time_t nowtime;
    im tim;

    pmb = vmb.begin();
    while (pmb !=vmb.end()){
        if (b == pmb->ckey){
            time (&nowtime);
            tim.mdate = nowtime;
            tim.rStatus = 0;
            tim.imid = a;

            //search for pre-existing payment msg
            pim = pmb->dim.begin();
            bool pop = true;
            while (pim !=pmb->dim.end()){
                if (pim->imid == a){
                    pmb->dim.erase(pim);
                    pop = false;
                    break;
                }
                pim++;
            }
            if (pop == true)
                pmb->dim.pop_back();
            pmb->dim.push_front(tim);
            break;
        }
        else
            pmb++;
    }
}

void mailController::checkInbox(string a){
    //Print Inbox
    pmb = vmb.begin();
    while (pmb !=vmb.end()){           
        if (a == pmb->ckey){
            bool cont=true;
            int c=999;
            while (c != 0){
                cout<<"********************************************"<<endl;
                cout<< a <<"'s Inbox"<<endl
                    <<left<< setw(10) << "Mail no."
                    << setw(10) << "Read?"
                    << setw(24) << "Mail Type"
                    << setw(28) << "Date Received"<<endl;

                pim = pmb->dim.begin();
                int i=1;
                while (pim !=pmb->dim.end()){
                    if (pim->mdate != 0){//will not print if no mail
                        cout << setw(10) << i;
                        if (pim->rStatus==true)
                            cout << setw(10) << "<Yes>";
                        else if (pim->rStatus==false)
                            cout << setw(10) << "<No>";
                        if (pim->imid == 1)
                            cout << setw(24) << "Booking Payment";
                        else if (pim->imid == 2)
                            cout << setw(24) << "Cancellation Payment";
                        else
                            cout << setw(24) << "Brochure";
                        printd(pim->mdate);
                    }
                    pim++;
                    i++;
                }
                cout << "\nSelect Mail no. to read (or 0 to exit): ";
                cin >> c;
                if (c ==0)
                    break;
                pim = pmb->dim.begin();
                pim = pim+c-1;
                if (pim->imid == 1){
                    cout << "\nOutstanding Package Payment.\n\n"
                         <<endl; //insert package amount.
                }

                if (pim->imid == 2){
                    cout << "\nOutstanding Cancellation Payment. O$P$\n\n"
                         <<endl; //insert package amount.
                }
                if (pim->imid == 3){
                    cout << "\nBrochure:\n\n";
                    pmm = vmm.begin();
                    while (pmm !=vmm.end()){
                        if (pmm->mid == pim->imid){
                            cout << pmm->msg <<endl;
                            break;
                        }
                        pmm++;
                    }
                }
                pim->rStatus = true;
            }
            break;
        }
        else
            pmb++;
    }
}
